<?php
class ControllerAccountQa extends Controller {
	
	public function index() {
		if (! $this->customer->isLogged ()) {
			$this->session->data ['redirect'] = $this->url->link ( 'account/return', '', 'SSL' );
			
			$this->redirect ( $this->url->link ( 'account/login', '', 'SSL' ) );
		}
		
		$this->language->load ( 'account/qa' );
		
		$this->document->setTitle ( $this->language->get ( 'heading_title' ) );
		$url = '';
		
		if (isset ( $this->request->get ['page'] )) {
			$url .= '&page=' . $this->request->get ['page'];
		}
		
		if (isset ( $this->request->get ['page'] )) {
			$page = $this->request->get ['page'];
		} else {
			$page = 1;
		}
		
		$this->data ['heading_title'] = $this->language->get ( 'heading_title' );
		
		$this->load->model ( 'account/qa' );
		
		$this->load->model ( 'tool/image' );
		
		$return_total = $this->model_account_qa->getTotalReviews ();
		
		$results = $this->model_account_qa->getReviews ( ($page - 1) * 10, 10 );
		
		$this->data ['products'] = array ();
		foreach ( $results as $result ) {
			
			if ($result ['image']) {
				$image = $this->model_tool_image->resize ( $result ['image'], $this->config->get ( 'config_image_wishlist_width' ), $this->config->get ( 'config_image_wishlist_height' ) );
			} else {
				$image = false;
			}
			$this->data ['products'] [] = array (
					'name' => $result ['name'],
					'text' => $result ['text'],
					'date' => $result ['date_added'],
					'img' => $image,
					'status' => $result ['status'] ? $this->language->get ( 'accept' ) : $this->language->get ( 'deny' ),
					'answer' => $result ['answer'],
					'answer_date' => $result ['answer_date'],
					'href' => $this->url->link ( 'product/product', 'product_id=' . $result ['product_id'] ) 
			);
		}
		
		$pagination = new Pagination ();
		$pagination->total = $return_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get ( 'config_catalog_limit' );
		$pagination->text = $this->language->get ( 'text_pagination' );
		$pagination->url = $this->url->link ( 'account/review', 'page={page}', 'SSL' );
		
		$this->data ['pagination'] = $pagination->render ();
		
		if (file_exists ( DIR_TEMPLATE . $this->config->get ( 'config_template' ) . '/template/account/qa.tpl' )) {
			$this->template = $this->config->get ( 'config_template' ) . '/template/account/qa.tpl';
		} else {
			$this->template = 'default/template/account/qa.tpl';
		}
		
		$this->children = array (
				'account/left',
				'common/footer',
				'common/header' 
		);
		
		$this->response->setOutput ( $this->render () );
	}
}